/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.filesystem;

import java.io.File;
import java.util.Vector;

/**
 *
 * @author triki
 */

public class SharedFolder {

    private String path;
    private Vector fileList;
    private String extension;
    
    private int n;

    public SharedFolder(String path)
    {
        fileList = new Vector();
        this.path = new String(path);

        this.loadUnfilteredList();
    }

    public SharedFolder(String path,String ext)
    {
        fileList = new Vector();
        this.path = new String(path);
        this.extension = new String(ext);
       
        this.loadFilteredList();
    }

    private void loadFilteredList()
    {
        int i,n;
        String[] aux;

        File f = new File(path);
        aux = f.list();

        i=0;
        while(true)
        {
            try
            {
                if(aux[i].endsWith(extension))
                {
                    fileList.add(aux[i]);
                }
                i++;
            }
            catch(IndexOutOfBoundsException ex)
            {
                break;
            }
        }
    }

    private void loadUnfilteredList()
    {
        int i;
        String[] aux;

        File f = new File(path);
        aux = f.list();
        i=0;
        
        while(true)
        {
            try
            {
                fileList.add(aux[i]);
                i++;
            }
            catch(IndexOutOfBoundsException ex)
            {
                break;
            }
        }
    }

    public String getPath()
    {
        return path;
    }

    public void refreshList()
    {
        if(extension == null)
            loadUnfilteredList();
        else
            loadFilteredList();
    }

    public void setFilter(String ext)
    {
        extension = new String(ext);
    }

    public void listFiles()
    {
        String aux = null;
        FileIterator fiter = new FileIterator(fileList);

        while(fiter.hasNext())
        {
            System.out.println(""+fiter.current());
            fiter.next();
        }
    }

    public boolean searchFile(String filename)
    {
        String aux = null;
        FileIterator fiter = new FileIterator(fileList);

        while(fiter.hasNext())
        {
            aux = fiter.current();
            if(aux.regionMatches(false, 0, filename, 0, filename.length()))
                return true;
            fiter.next();
        }
        return false;
    }

    public FileIterator getIterator()
    {
        return new FileIterator(fileList);
    }
}
